//
//  NWStatusToolBar.swift
//  Weibo
//
//  Created by Northwan on 16/1/25.
//  Copyright © 2016年 Northwan. All rights reserved.
//

import UIKit

class NWStatusToolBar: UIView {
    
    var retweetButton: UIButton?
    var commentButton: UIButton?
    var unlikeButton: UIButton?

    var statusViewModel:NWStatusViewModel?{
        didSet{
            retweetButton?.setTitle(statusViewModel?.repostsCountString, forState: .Normal)
            commentButton?.setTitle(statusViewModel?.commentsCountString, forState: .Normal)
            unlikeButton?.setTitle(statusViewModel?.attitudesCountString, forState: .Normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        retweetButton =  setupBottomButton("转发", imgName: "timeline_icon_retweet")
        commentButton =   setupBottomButton("评论", imgName: "timeline_icon_comment")
        unlikeButton =   setupBottomButton("赞", imgName: "timeline_icon_unlike")
        
        
        retweetButton?.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        commentButton?.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.width.equalTo(unlikeButton!)
        }
        unlikeButton?.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.trailing.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        let sp1 = setupSepector()
        let sp2 = setupSepector()
        
        sp1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
        
        sp2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
    }
    
    private func setupBottomButton(title:String,imgName:String)->UIButton{
        let button = UIButton()
        button.setTitle(title, forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Highlighted)
        button.setImage(UIImage(named: imgName), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: .Highlighted)
        addSubview(button)
        return button
    }
    
    private func setupSepector()->UIImageView{
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }
    
}
